﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gype.UI;
using System.Runtime.InteropServices;
using SKYPE4COMLib;

namespace Gype.GUI.Components
{
    public class MainFrame : ItemsControl
    {
        public void SetLoggedInUser(User loggedInUser)
        {
            if (loggedInUser == null)
                Text = "Gype - not logged in";
            else
                Text = "Gype - Logged in as '" + loggedInUser.FullName + "'";
        }

        internal override void AttachedToManager(Manager mgr)
        {
            base.AttachedToManager(mgr);

            initResources();
            Text = "Gype - not logged in";

            mMainTab = new MainTab();
            mMainTab.AttachToFrame(this);
            addChild(mMainTab);
        }

        private bool hitExpander(float sx, float sy)
        {
            float dx = sx - (Size.Width + 12);
            float dy = sy - (Size.Height / 2);

            return (dx * dx + dy * dy) <= 400.0f;
        }

        internal override void OnMessage(ControlMessage message)
        {
            base.OnMessage(message);
            if (message is MouseMessage)
            {
                MouseMessage mmsg = message as MouseMessage;
                switch (mmsg.Message)
                {
                    case Win32Native.WM.LBUTTONDOWN:
                        mIsExpanderDown = hitExpander(mmsg.X, mmsg.Y);
                        break;

                    case Win32Native.WM.LBUTTONUP:
                        {
                            if (hitExpander(mmsg.X, mmsg.Y) && mIsExpanderDown)
                            {
                                mIsExpanded = !mIsExpanded;
                                Size = new System.Drawing.SizeF(mIsExpanded ? 600 : 306, Size.Height);
                            }

                            mIsExpanderDown = false;
                        }
                        break;
                }
            }
        }

        internal override void RenderObject()
        {
            var loc = Location;
            var target = mGUIManager.Target;

            target.FillRectangle(new SlimDX.Vector2(loc.X + 3, loc.Y + 27), new SlimDX.Vector2(Size.Width - 6, Size.Height - 28), BrushCollection.FrameBackground);

            renderChildren();

            target.FillRectangle(new SlimDX.Vector2(loc.X + 303, loc.Y + 27), new SlimDX.Vector2(1, Size.Height - 28), BrushCollection.FrameBorder);

            target.DrawRoundedRectangle(new SlimDX.Vector2(loc.X, loc.Y), new SlimDX.Vector2(Size.Width, Size.Height),
                5, 5, BrushCollection.FrameBorder, 3);

            target.FillRoundedRectangle(new SlimDX.Vector2(loc.X + 1, loc.Y), new SlimDX.Vector2(Size.Width - 2, 7),
                5, 5, BrushCollection.FrameBorder);

            target.FillRectangle(new SlimDX.Vector2(loc.X, loc.Y + 7), new SlimDX.Vector2(Size.Width + 1, 20), BrushCollection.FrameBorder);

            target.FillRoundedRectangle(new SlimDX.Vector2(loc.X + Size.Width - 1, loc.Y + Size.Height / 2 - 30), new SlimDX.Vector2(25, 60), 4, 4, BrushCollection.FrameBorder);
            target.FillRectangle(new SlimDX.Vector2(loc.X + Size.Width - 3, loc.Y + Size.Height / 2 - 30), new SlimDX.Vector2(15, 60), BrushCollection.FrameBorder);
            
            target.DrawImage(mIsExpanded ? ImageCollection.ArrowCollapse : ImageCollection.ArrowExpand, new System.Drawing.RectangleF(loc.X + Size.Width + 2, loc.Y + Size.Height / 2 - 10, 20, 20));

            target.DrawImage(mAppIcon, new System.Drawing.RectangleF(loc.X + 6, loc.Y + 2, 23, 23));

            if (!string.IsNullOrEmpty(Text))
            {
                mCaptionText.DrawText(Text, new SlimDX.Vector2(loc.X + 34, loc.Y + 5), new SlimDX.Vector2(Size.Width - 34 - 35, 16));
            }

            renderChildrenTopLevel();
        }

        internal override void RenderTopLevel()
        {
        }

        internal override System.Drawing.PointF getChildOffset()
        {
            return new System.Drawing.PointF(3 + mNCExtent.X, 27 + mNCExtent.Y);
        }

        private void initResources()
        {
            mAppIcon = Image.FromGDI(mGUIManager.Target, Images.GypeIconMain);
            mCaptionText = new TextElement(TextCollection.Minya24);
            mCaptionText.Color = BrushCollection.FrameCaptionText;
            mCaptionText.Alignment = TextAlignement.Left;
        }

        public ContactList ContactList { get { return mMainTab.ContactList; } }
        public ConvList Conversations { get { return mMainTab.Conversations; } }

        public string Text { get; set; }

        private Image mAppIcon;
        private TextElement mCaptionText;
        private MainTab mMainTab;
        private User mCurrentUser;
        private bool mIsExpanded = true;
        private bool mIsExpanderDown = false;
    }
}
